package com.itheima.servletlet;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

@WebServlet("/loginStudentservlet")
public class LoginStudentservlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //获取请求用户名
        String username = req.getParameter("username");
        //获取请求密码
        String password = req.getParameter("password");

        //判断用户名是否为空为空就返回("/login.jsp"),不为空添加到会话当中,并返回("/index.jsp")
        if (username==null|| "".equals(username)){
            resp.sendRedirect("/login.jsp");
            return;
        }
        HttpSession session = req.getSession();
        session.setAttribute("username",username);
        resp.sendRedirect("/index.jsp");
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}
